<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	private $dbAdapter;
	private $fileAdapter;
	private $config;
	private $cache;
	private $authAdapter;
	private $acl;
	private $logger;
	
    protected function _initAppAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'App',
            'basePath'  => dirname(__FILE__),
        ));
        return $autoloader;
    }
    
	protected function _initConfig()
    {
    	 $this->config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', 'production');
    	 Zend_Registry::set('Zend_Config_Ini', $this->config);
    }
    protected function _initLog()
    {
    	$writer 		= new Zend_Log_Writer_Stream($this->config->includePaths->log.'default.log');
    	
    	$writer->setFormatter(new Zend_Log_Formatter_Xml());
    	$this->logger	= new Zend_Log($writer);
    	
    	//$this->logger->setEventItem('timestamp', date('D, j M Y H:i:s', time()));
    	//$this->logger->setEventItem('message', '<![CDATA[message]]>');
    	
    	Zend_Registry::set('Zend_Log', $this->logger);
    }
	protected function _initLayoutHelper()
    {
    	$this->bootstrap('frontController');
    	$layout = Zend_Controller_Action_HelperBroker::addHelper(new Pushandplay_Controller_Action_Helper_LayoutLoader());
    }
	protected function _initDb()
	{
        $this->dbAdapter = Zend_Db::factory($this->config->resources->db->adapter, $this->config->resources->db->params->toArray()); 
        Zend_Db_Table::setDefaultAdapter($this->dbAdapter); 
	}
    protected function _initCache()
	{
		$this->cache	= Zend_Cache::factory('Core', 'File', $this->config->cache->frontendOptions->toArray(), $this->config->cache->backendOptions->toArray());
		Zend_Registry::set('Zend_Cache', $this->cache);
	} 
	protected  function _initSessions()
	{
		Zend_Session::setOptions($this->config->session->toArray());
		Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($this->config->sessionhandler->toArray()));
		
		/*$sh = Zend_Session::getSaveHandler();
		$sh->gc($config->sessionhandler->lifetime);
		$this->sessionNameSpace = new Zend_Session_Namespace('pushandplay-cms');*/
	}
	protected  function _initAuth()
	{
		//$auth				= Zend_Auth::getInstance();
		$this->authAdapter	= new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter(), 'users', 'email', 'password', "?");
		
		Zend_Registry::set('Zend_Auth', $this->authAdapter);
	}
	protected function _initFile()
	{
	    $this->fileAdapter    = new Zend_File_Transfer_Adapter_Http();
	    $this->fileAdapter->setDestination($this->config->includePaths->files);
	    
	    Zend_Registry::set('Zend_File', $this->fileAdapter);
	}
	protected function _initAcl()
	{
		$this->acl = new Zend_Acl();
		
		$this->acl->addRole(new Zend_Acl_Role('member'));
	    $this->acl->addRole(new Zend_Acl_Role('admin'), 'member');
	    
		$this->acl->add(new Zend_Acl_Resource('default:index:access'));
		$this->acl->allow('admin', 'default:index:access', null);
		
		Zend_Registry::set('Zend_Acl', $this->acl);
	}
}